{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": [],
      "authorship_tag": "ABX9TyMdDMDSqfgffWcCf0zTS8yd",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/mshumer/gpt-prompt-engineer/blob/main/gpt_planner.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install openai\n",
        "\n",
        "OPENAI_API_KEY = \"YOUR API KEY\""
      ],
      "metadata": {
        "id": "nNAx9eT3ule2"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "tf_S5R1Muafd"
      },
      "outputs": [],
      "source": [
        "import openai\n",
        "import random\n",
        "\n",
        "client = openai.OpenAI(api_key=OPENAI_API_KEY)\n",
        "\n",
        "def generate_plans(user_query, n=5):\n",
        "    \"\"\"Generate multiple plans using GPT-4o-Mini.\"\"\"\n",
        "    response = client.chat.completions.create(\n",
        "        model=\"gpt-4o-mini\",\n",
        "        messages=[\n",
        "            {\"role\": \"system\", \"content\": \"You are a strategic reasoner. Given a user query, create a detailed plan to address it and then respond to the user. You will do so by using <thinking> and <response> tags. The user will only see what is in <response>, the <thinking> is just a scratchpad for you to figure out the best approach to satisfy the user's request. First, plan inside <thinking> tags, and then write your <response>.\"},\n",
        "            {\"role\": \"user\", \"content\": user_query}\n",
        "        ],\n",
        "        n=n,\n",
        "        temperature=0.7,\n",
        "        max_tokens=500,\n",
        "        stop=['</thinking>']\n",
        "    )\n",
        "    return [choice.message.content.replace('<thinking>', '') for choice in response.choices]\n",
        "\n",
        "def evaluate_plans(plans, user_query):\n",
        "    \"\"\"Evaluate plans using a tournament ranking system.\"\"\"\n",
        "    def compare_plans(plan1, plan2):\n",
        "        response = client.chat.completions.create(\n",
        "            model=\"gpt-4o-mini\",\n",
        "            messages=[\n",
        "                {\"role\": \"system\", \"content\": \"You are a judge evaluating two plans. Choose the better plan based on effectiveness, feasibility, and relevance to the user's query.\"},\n",
        "                {\"role\": \"user\", \"content\": f\"User Query: {user_query}\\n\\nPlan 1: {plan1}\\n\\nPlan 2: {plan2}\\n\\nWhich plan is better? Respond with either '1' or '2'.\"}\n",
        "            ],\n",
        "            temperature=0.2,\n",
        "            max_tokens=10\n",
        "        )\n",
        "        return 1 if response.choices[0].message.content.strip() == \"1\" else 2\n",
        "\n",
        "    # Tournament ranking — to improve, run twice with swapped positions for each plan combo for greater precision, and parallelize for speed\n",
        "    winners = plans\n",
        "    while len(winners) > 1:\n",
        "        next_round = []\n",
        "        for i in range(0, len(winners), 2):\n",
        "            if i + 1 < len(winners):\n",
        "                winner = winners[i] if compare_plans(winners[i], winners[i+1]) == 1 else winners[i+1]\n",
        "                next_round.append(winner)\n",
        "            else:\n",
        "                next_round.append(winners[i])\n",
        "        winners = next_round\n",
        "\n",
        "    return winners[0]\n",
        "\n",
        "def generate_response(best_plan, user_query):\n",
        "    \"\"\"Generate the final response based on the best plan.\"\"\"\n",
        "    response = client.chat.completions.create(\n",
        "        model=\"gpt-4o-mini\",\n",
        "        messages=[\n",
        "            {\"role\": \"system\", \"content\": \"You are a helpful assistant. Use the given plan to create a detailed and high-quality response to the user's query.\"},\n",
        "            {\"role\": \"user\", \"content\": f\"User Query: {user_query}\\n\\nPlan: {best_plan}\\n\\nGenerate a detailed response based on this plan.\"}\n",
        "        ],\n",
        "        temperature=0.5,\n",
        "        max_tokens=2000\n",
        "    )\n",
        "    return response.choices[0].message.content\n",
        "\n",
        "def improved_ai_output(user_query, num_plans=20):\n",
        "    \"\"\"Main function to improve AI output quality.\"\"\"\n",
        "    print(\"Generating plans...\")\n",
        "    plans = generate_plans(user_query, n=num_plans)\n",
        "\n",
        "    print(\"Evaluating plans...\")\n",
        "    best_plan = evaluate_plans(plans, user_query)\n",
        "\n",
        "    print(\"Generating final response...\")\n",
        "    final_response = generate_response(best_plan, user_query)\n",
        "\n",
        "    return {\n",
        "        \"user_query\": user_query,\n",
        "        \"best_plan\": best_plan,\n",
        "        \"final_response\": final_response\n",
        "    }\n",
        "\n",
        "if __name__ == \"__main__\":\n",
        "    user_query = \"How do I get a computer out of a bootloop?\"\n",
        "    result = improved_ai_output(user_query)\n",
        "\n",
        "    print(\"\\nUser Query:\", result[\"user_query\"])\n",
        "    print(\"\\nBest Plan:\", result[\"best_plan\"])\n",
        "    print(\"\\nFinal Response:\", result[\"final_response\"])"
      ]
    }
  ]
}